function info(message, status) {
	var ts = document.querySelector('.TS');
	switch(status) {
		case 0:
			ts.style = '';
			break;
		case 1:
			ts.style.backgroundColor = 'rgba(19,198,191,.8)';
			break;
		default:
			break;
	}
	ts.innerHTML = message;
	ts.style.opacity = 1;
	setTimeout(function() {
		ts.style.opacity = 0;
	}, 2000)
}

var mask = document.querySelector(".mask");
window.onload = function() {
	var ts = document.querySelector('.TS');
	var reg = /^1[3-9][0-9]{9}$/;
	var input = document.getElementById('phonenumber');
	input.style.background = "#ececec";
	input.onblur = function(e) {
		e.preventDefault();
		input.style.background = "#ececec";
		if(!reg.test(this.value)) {
			info("请输入正确的手机号", 0);
		}
	};
	//联系客服
	var connect = document.querySelector("header span");
	Tap(connect, function() {
		window.location.href = "../service.html";
	})
	//点击登陆
	var denglu = document.getElementsByClassName('denglu')[0];
	//Tap(denglu,function(){
	//    enter();
	//});
	denglu.addEventListener("touchstart", function() {
		enter();
		denglu.addEventListener("touchend", function(e) {
			e.preventDefault();
		})
	});
	document.addEventListener("keyup", function(e) {
		if(e.keyCode == 13) {
			enter();
		}
	});

	function enter() {
		var password = document.getElementById('password');
		var token;
		if(input.value && password.value) {
			mask.style.display = "block";
			var username = input.value;
			var password = password.value;
			$.ajax({
				type: "POST",
				url: pathName + "/oauth/access_token",
				async: false,
				data: {
					"grant_type": "password",
					"client_id": "f3d259ddd3ed8ff3843839b",
					"client_secret": "Nqs4CyAV7htrgEROJUah6u4C9EvqStFpP7H5HAwW",
					"username": username,
					"password": password,
					"scope": "",
				},
				datatype: "json",
				success: function(obj) {
					console.log(obj);
					//ajax请求个人信息判断是后台是否审核通过
					token = obj.access_token;

					//addKeyValue("like_token", token, 1, function () {
					//    console.log(token);
					//    window.location.href = '../../index.html';
					//});
				},
				error: function(res) {
					console.log(res);
					var obj = JSON.parse(res.responseText);
					var code = parseInt(obj.code);
					mask.style.display = "none";
					if(code == 20001) {
						info("该账号不存在", 0);
					} else if(code == 20002) {
						info("该账号正在等待审核", 0);
					} else {
						info("账号或密码错误", 0);
					}
				}
			});
			if(token) {
				$.ajax({
					url: pathName + "/api/v1/member",
					type: "GET",
					dataType: "json",
					headers: {
						"Accept": "application/json",
						"Authorization": "Bearer " + token
					},
					async: false,
					success: function(data) {
						console.log(data);
						mask.style.display = "none";
						if(data.member.isLock == 0) {
							addKeyValue("like_token", token, 1, function() {
								console.log(token);
								window.location.href = '../../index.html';
							});
						} else if(data.member.isLock == 2) {
							localStorage.setItem("error_token", token);
							ts.innerHTML = '该账号审核未通过，请完善信息';
							ts.style.opacity = 1;
							setTimeout(function() {
								ts.style.opacity = 0;
								window.location.href = 'licence.html';
							}, 2000);
						} else if(data.member.isLock == 1) {
							info("该账号正在等待审核", 0);
						}
					},
					error: function(data) {
						mask.style.display = "none";
						alert(data.readyState);
						info("网络连接出错，请重试", 0);
					}
				})
			}
		} else {
			info("请完善信息", 0);
		}
	}

}
